﻿using CloudFranceTriple.Models;
using CloudFranceTriple.WebRole.Models;
using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CloudFranceTriple.WebRole.Areas.Forum.Controllers
{
    [CloudFranceTriple.WebRole.Filters.InitializeSimpleMembership]
    public class SecurityController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        //
        // GET: /Forum/Security/

        [Authorize(Roles = "RoleMember,RoleFans,RoleBaffMember,RoleAdminForum")]
        public ActionResult Index(int id = 0)
        {
            var forumsecurityset = db.ForumSecuritySet.Where(f => f.ForumSet_Id == id).Include(f1 => f1.ForumSet).Include(f2 => f2.webpages_Roles);
            ViewBag.ForumSet_Id = id;
            ViewBag.LabelForum = HttpUtility.HtmlDecode(db.ForumSet.Find(id).LabelForum);
            var HowManyRolesAreUnsed = db.webpages_Roles.Except(db.webpages_Roles.Join(db.ForumSecuritySet.Where(f3 => f3.ForumSet_Id == id), r1 => r1.RoleId, s1 => s1.webpages_Roles_RoleId, (r1, s1) => r1)).Count();
            ViewBag.ShowForumSecuritySetAddButton = (HowManyRolesAreUnsed > 0) as Boolean?;
            return View(forumsecurityset.ToList());
        }

        //
        // GET: /Forum/Security/Create

        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult Create(int id = 0)
        {
            ViewBag.ForumSet_Id = id;
            ViewBag.LabelForum = HttpUtility.HtmlDecode(db.ForumSet.Find(id).LabelForum);
            // OuterJoin with Linq Lambda Expression based on sequence1.except(sequence2)
            var RoleNotInUsedInThisForumSet = db.webpages_Roles.Except(db.webpages_Roles.Join(db.ForumSecuritySet.Where(f3 => f3.ForumSet_Id == id), r1 => r1.RoleId, s1 => s1.webpages_Roles_RoleId, (r1, s1) => r1));
            ViewBag.webpages_Roles_RoleId = new SelectList(RoleNotInUsedInThisForumSet, "RoleId", "RoleName");
            ViewBag.RightLevel = new SelectList(RightLevel.GetRightLevelSelectListItems(), "Value", "Text");
            return View();
        }

        //
        // POST: /Forum/Security/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult Create(ForumSecuritySet forumsecurityset)
        {
            if (ModelState.IsValid)
            {
                db.ForumSecuritySet.Add(forumsecurityset);
                db.SaveChanges();
                return RedirectToAction("Index", "Security", new { area = "forum", id = forumsecurityset.ForumSet_Id });
            }

            ViewBag.ForumSet_Id = forumsecurityset.ForumSet_Id;
            ViewBag.LabelForum = HttpUtility.HtmlDecode(db.ForumSet.Find(forumsecurityset.ForumSet_Id).LabelForum);
            ViewBag.webpages_Roles_RoleId = new SelectList(db.webpages_Roles, "RoleId", "RoleName", forumsecurityset.webpages_Roles_RoleId);
            return View(forumsecurityset);
        }

        //
        // GET: /Forum/Security/Edit/5

        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult Edit(int id = 0, int id2 = 0)
        {
            ForumSecuritySet forumsecurityset = db.ForumSecuritySet.Find(id, id2);
            if (forumsecurityset == null)
            {
                return HttpNotFound();
            }
            ViewBag.ForumSet_IdList = new SelectList(db.ForumSet, "Id", "LabelForum", forumsecurityset.ForumSet_Id);
            ViewBag.ForumSet_Id = forumsecurityset.ForumSet_Id;
            ViewBag.LabelForum = HttpUtility.HtmlDecode(db.ForumSet.Find(forumsecurityset.ForumSet_Id).LabelForum);
            ViewBag.RightLevel = new SelectList(RightLevel.GetRightLevelSelectListItems(), "Value", "Text", forumsecurityset.RightLevel);
            ViewBag.webpages_Roles_RoleId = new SelectList(db.webpages_Roles, "RoleId", "RoleName", forumsecurityset.webpages_Roles_RoleId);
            return View(forumsecurityset);
        }

        //
        // POST: /Forum/Security/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult Edit(ForumSecuritySet forumsecurityset)
        {
            if (ModelState.IsValid)
            {
                db.Entry(forumsecurityset).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", "Security", new { area = "forum", id = forumsecurityset.ForumSet_Id });
            }
            ViewBag.ForumSet_IdList = new SelectList(db.ForumSet, "Id", "LabelForum", forumsecurityset.ForumSet_Id);
            ViewBag.ForumSet_Id = forumsecurityset.ForumSet_Id;
            ViewBag.LabelForum = HttpUtility.HtmlDecode(db.ForumSet.Find(forumsecurityset.ForumSet_Id).LabelForum);
            ViewBag.webpages_Roles_RoleId = new SelectList(db.webpages_Roles, "RoleId", "RoleName", forumsecurityset.webpages_Roles_RoleId);
            return View(forumsecurityset);
        }

        //
        // GET: /Forum/Security/Delete/5

        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult Delete(int id = 0, int id2 = 0)
        {
            ForumSecuritySet forumsecurityset = db.ForumSecuritySet.Find(id, id2);
            ViewBag.ForumSet_Id = id;
            ViewBag.LabelForum = HttpUtility.HtmlDecode(db.ForumSet.Find(id).LabelForum);
            if (forumsecurityset == null)
            {
                return HttpNotFound();
            }
            return View(forumsecurityset);
        }

        //
        // POST: /Forum/Security/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminForum")]
        public ActionResult DeleteConfirmed(int id, int id2 = 0)
        {
            ForumSecuritySet forumsecurityset = db.ForumSecuritySet.Find(id, id2);
            ViewBag.ForumSet_Id = id;
            ViewBag.LabelForum = HttpUtility.HtmlDecode(db.ForumSet.Find(id).LabelForum);
            db.ForumSecuritySet.Remove(forumsecurityset);
            db.SaveChanges();
            return RedirectToAction("Index", "Security", new { area = "forum", id = id });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}